package com.ramos.kuaixiao.adapter;

import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.entity.ShopTypeBean;
import com.ramos.kuaixiao.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class ParentAdapter extends BaseExpandableListAdapter {
    private String TAG ="ParentAdapter";

    private Context mContext;// 上下文

    private List<ShopTypeBean.ShopType.Type> mParents = new ArrayList<>();// 数据源

    public ParentAdapter(Context context, List<ShopTypeBean.ShopType.Type> parents) {
        this.mContext = context;
        this.mParents = parents;
        for (ShopTypeBean.ShopType.Type data : mParents) {
            Log.d(TAG, "ParentAdapter " + data.getTypeName() + data.getTypeGroupList().size());
        }
    }

    // 获得某个父项的某个子项
    @Override
    public ShopTypeBean.ShopType.Product getChild(int groupPosition, int childPosition) {
        if (mParents.get(groupPosition).getTypeGroupList() == null) {
            return null;
        } else {
            Log.d(TAG, "ParentAdapter :getChild:" + mParents.get(groupPosition).getTypeGroupList().get(childPosition).getTypeId()
            +" " + mParents.get(groupPosition).getTypeGroupList().get(childPosition).getTypeName()
            +" " + mParents.get(groupPosition).getTypeGroupList().get(childPosition).getProductInfoNameList().size());
            return mParents.get(groupPosition).getTypeGroupList().get(childPosition);

        }
    }

    // 获得某个父项的某个子项的id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    // 获得某个父项的子项数目
    @Override
    public int getChildrenCount(int groupPosition) {
        return mParents.get(groupPosition).getTypeGroupList() != null ? mParents.get(groupPosition).getTypeGroupList().size() : 0;
    }

    public void refresh(List<ShopTypeBean.ShopType.Type> tasks) {
        this.mParents = tasks;
        notifyDataSetChanged();
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isExpanded, View convertView,
                             ViewGroup parent) {

        final ShopTypeBean.ShopType.Product child = getChild(groupPosition, childPosition);
        if (child != null) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.list_task_my, null);
                holder = new ViewHolder();
                holder.tvShopType = (TextView) convertView.findViewById(R.id.tvShopType);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if(StringUtil.isEmpty(child.getTypeName())){
                holder.tvShopType.setText("其它");
            } else {
                holder.tvShopType.setText(child.getTypeName());
            }

        }
        return convertView;
    }

    static class ViewHolder {
        TextView tvShopType;
    }


    // 获得某个父项
    @Override
    public Object getGroup(int groupPosition) {
        return mParents.get(groupPosition);
    }

    // 获得父项的数量
    @Override
    public int getGroupCount() {
        return mParents != null ? mParents.size() : 0;
    }

    // 获得某个父项的id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupholder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_task, parent, false);
            groupholder = new GroupHolder();
            groupholder.tvShops = (TextView) convertView.findViewById(R.id.tvShops);
            groupholder.ivSelect = (ImageView) convertView.findViewById(R.id.iv_select);
            convertView.setTag(groupholder);
        } else {
            groupholder = (GroupHolder) convertView.getTag();
        }
        groupholder.tvShops.setText(mParents.get(groupPosition).getTypeName());
        if (isExpanded) {
            groupholder.ivSelect.setImageResource(R.drawable.ic_arrow_top);
        } else {
            groupholder.ivSelect.setImageResource(R.drawable.ic_arrow_down);
        }
        Log.d(TAG, "getGroupView:"+groupPosition+"  "+mParents.get(groupPosition).getTypeName());

        return convertView;
    }

    //                用于存放Indicator的集合
    private SparseArray<ImageView> mIndicators = new SparseArray<>();
    //            根据分组的展开闭合状态设置指示器
    public void setIndicatorState(int groupPosition, boolean isExpanded) {
        if (isExpanded) {
            mIndicators.get(groupPosition).setImageResource(R.drawable.ic_arrow_top);
        } else {
            mIndicators.get(groupPosition).setImageResource(R.drawable.ic_arrow_down);
        }
    }


    class GroupHolder {

        TextView tvShops;
        ImageView ivSelect;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
